#include <avr/io.h>

#include "uart.h"

/*
 * uart.c
 *
 *  Created on: Jun 15, 2011
 *      Author: ben
 */

void USARTInit(uint16_t ubrr_value)
{
	UBRR0L = ubrr_value;
	UBRR0H = (ubrr_value >> 8);
	UCSR0B = (1 << RXEN0) | (1 << TXEN0);
	UCSR0C = (1 << USBS0) | (1 << UCSZ01) | (1 << UCSZ00);
}

char USARTReadChar()
{
	while(!(UCSR0A & (1 << RXC0)))
	{
		//Do Nothing
	}

	return UDR0;
}

void USARTWriteChar(char data)
{
	while(!(UCSR0A & (1 << UDRE0)))
	{
		//Do Nothing
	}

	UDR0 = data;
}

void USARTWriteString(char* data)
{
	int length = strlen(data);
	for (int i = 0; i < length; i++)
	{
		USARTWriteChar(data[i]);
	}
}
